package com.xxx.video.mine.adapters;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;


import com.xxx.video.R;
import com.xxx.video.commen.adapters.BaseRecycleViewAdapter;
import com.xxx.video.commen.adapters.BaseViewHolder;
import com.xxx.video.commen.dbs.beans.SearchHistoryBean;
import com.xxx.video.commen.utils.ListUtils;
import com.xxx.video.commen.views.TagAdapter;
import com.xxx.video.commen.views.TagLayout;
import com.xxx.video.mine.beans.SearchHotWordListBean;

import java.util.List;

import butterknife.BindView;

public class SearchRecommendListAdapter extends BaseRecycleViewAdapter<Object, BaseViewHolder> {
    /**
     * search hot word list
     */
    private List<SearchHotWordListBean.ListBean> searchHotWordList;
    /**
     * search history list
     */
    private List<SearchHistoryBean> searchHistoryBeanList;

    public SearchRecommendListAdapter(@NonNull Context context) {
        super(context);
    }

    @Override
    public BaseViewHolder onCreateDefaultViewHolder(View view, int viewType) {
        if (viewType == 0) {
            return new SearchRecommendHotSearchHolder(view);
        } else {
            return new SearchRecommendHistoryHolder(view);
        }
    }

    @Override
    public int getItemLayout(int viewType) {
        if (viewType == 0) {
            return R.layout.item_search_recommend_hot_search_list;
        } else {
            return R.layout.item_search_recommend_history_list;
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        int count = 0;
        if (!ListUtils.isEmpty(searchHotWordList)) {
            count++;
        }
        if (!ListUtils.isEmpty(searchHistoryBeanList)) {
            count++;
        }
        return count;
    }

    @Override
    public boolean isSetBaseItemViewClickListener() {
        return false;
    }

    /**
     * set search hot word
     *
     * @param list data
     */
    public void setSearchHotWordListData(List<SearchHotWordListBean.ListBean> list) {
        this.searchHotWordList = list;
        notifyDataSetChanged();
    }

    /**
     * set search history list
     *
     * @param searchHistoryBeanList data
     */
    public void setSearchHistoryListData(List<SearchHistoryBean> searchHistoryBeanList) {
        this.searchHistoryBeanList = searchHistoryBeanList;
        notifyDataSetChanged();
    }

    public class SearchRecommendHotSearchHolder extends BaseViewHolder {
        @BindView(R.id.tv_item_search_recommend_list_hot_search_tile)
        TextView tvItemSearchRecommendListHotSearchTile;
        @BindView(R.id.tl_item_search_recommend_list_hot_search_list)
        TagLayout tlItemSearchRecommendListHotSearchList;

        public SearchRecommendHotSearchHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void setData2View(int position) {
            if (ListUtils.isEmpty(searchHotWordList)) {
                return;
            }
            SearchHotWordListAdapter searchHotWordListAdapter = new SearchHotWordListAdapter(searchHotWordList);
            tlItemSearchRecommendListHotSearchList.setAdapter(searchHotWordListAdapter);
            searchHotWordListAdapter.setOnItemClickListener((TagAdapter.OnItemClickListener<SearchHotWordListBean.ListBean>) (listBean, position1, type) -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(listBean, position, 2);
                }
            });
        }
    }

    public class SearchRecommendHistoryHolder extends BaseViewHolder {
        @BindView(R.id.tl_item_search_recommend_list_history_list)
        TagLayout tlItemSearchRecommendListHistoryList;

        @BindView(R.id.tv_item_search_recommend_list_history_clear)
        TextView tvItemSearchRecommendListHistoryClear;

        public SearchRecommendHistoryHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void setData2View(int position) {
            if (ListUtils.isEmpty(searchHistoryBeanList)) {
                return;
            }
            SearchHistoryListAdapter searchHistoryListAdapter = new SearchHistoryListAdapter(searchHistoryBeanList);
            tlItemSearchRecommendListHistoryList.setAdapter(searchHistoryListAdapter);
            searchHistoryListAdapter.setOnItemClickListener((TagAdapter.OnItemClickListener<SearchHistoryBean>) (searchHistoryBean, position1, type) -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(searchHistoryBean, position, 3);
                }
            });
            tvItemSearchRecommendListHistoryClear.setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(null, position, 4);
                }
            });
        }
    }
}
